package com.qianclass.talklive.app.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.hotwater.uacclient.IInfoAccess;
/**
 * Created by chengwenqiang on 2019/9/11.
 */

public class InfoAccessImpl extends IInfoAccess.Stub{


    private static final String TAG = "InfoAccessImpl";

    //设备唯一标示
    public static final String DEVICEID = "DeviceID";

    //服务器获取的token
    public static final String TOKEN_ID = "TOKEN_ID";

    //用于通信的hottvid
    public static final String HOTTV_ID = "HOTTV_ID";

    private static InfoAccessImpl instance;

    private DoCallback mCallback;

    private Context context;

    private Boolean mIsBind =false;

    private com.hotwater.uacclient.IInfoAccess mDataService;

    private String mHottvId ="";

    public InfoAccessImpl(Context context) {
        this.context = context;

        bindAuthService();
    }

    public static InfoAccessImpl getInstance(Context context) {
        if (null == instance) {
            synchronized (InfoAccessImpl.class) {
                if (null == instance) {
                    instance = new InfoAccessImpl(context);
                }
            }
        }
        return instance;
    }

    private void bindAuthService(){
        Intent intent = new Intent("com.hotwater.uacclient.IInfoAccess");
        intent.setPackage("com.hotwater.uacclient");
        mIsBind = this.context.bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }

    public String  getHottvId()
    {
        if(!TextUtils.isEmpty(mHottvId))
            return mHottvId;

        try{
            mHottvId =  getInfoData(HOTTV_ID,"");
        }catch (RemoteException e)
        {
            e.printStackTrace();
        }

        Log.i(TAG,"HOTTV_ID:"+mHottvId);

        return  mHottvId;
    }

    public String  getTokenId()
    {
        String token ="";

        try{
            token =  getInfoData(TOKEN_ID,"");
        }catch (RemoteException e)
        {
            e.printStackTrace();
        }

        Log.i(TAG,"token:"+token);

        return  token;
    }

    public void unbindAuthService()
    {
        mHottvId ="";
        if(mIsBind) {
            mIsBind =false;
            this.context.unbindService(conn);
        }
    }

    public void setDoCallBack(DoCallback callBack)
    {
        mCallback = callBack;
        if(mDataService!=null)
        {
            if(mCallback!=null)
            {
                mCallback.onSuccess("");
            }
        }
    }

    private ServiceConnection conn = new ServiceConnection() {
        /** 获取服务对象时的操作 */
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            Log.i(TAG,"onServiceConnected");
            mDataService = IInfoAccess.Stub.asInterface(service);
            if(mCallback!=null)
            {
                mCallback.onSuccess("");
            }
        }

        /** 无法获取到服务对象时的操作 */
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            Log.i(TAG,"onServiceDisconnected");
            mDataService = null;
            if(mCallback!=null)
            {
                mCallback.onFailor("");
            }
        }
    };

    @Override
    public String getInfoData(String dataName, String extData) throws RemoteException {

        Log.i(TAG,"getSTBData"+dataName + " extData " + extData);
        if(mDataService!=null)
        {
            String value =  mDataService.getInfoData(dataName,extData);
            Log.i(TAG,"getSTBData:"+dataName+",value:"+value);
            return value;
        }else {
            bindAuthService(); //绑定服务
        }
        return "";
    }

}
